﻿using AutoMapper;
using Domain.Flow;
using MediatR;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command.RabbitBWss;

namespace U.B.WriteApi.CommandHander.RabbitBWDD
{
    public class AddFarmCommmandHanderL : IRequestHandler<AddFarmCommmand, ApiResult<object>>
    {
        private readonly IRepistory<Farm> _farmRepository;
        private readonly IMapper _mapper;

        public AddFarmCommmandHanderL(IRepistory<Farm> farmRepository, IMapper mapper)
        {
            _farmRepository = farmRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 养殖信息添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(AddFarmCommmand request, CancellationToken cancellationToken)
        {
            ApiResult<object> apiResult = new ApiResult<object>();
            var result= _mapper.Map<Farm>(request);
            _farmRepository.Add(result);
            apiResult.Data = result;
            apiResult.code = Renum.添加成功;
            apiResult.Message = Renum.添加成功.ToString();
            return Task.FromResult(apiResult);
        }
    }
}
